package atomic.LongAccumulator;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.LongAccumulator;

/**
 * @author ZhuQi
 * @ClassName LongAccumulatorDemo
 * @Date 2022/9/16 15:53
 * @Version 1.0
 * @Description 测试 LongAccumulatorDemo
 */
@Slf4j
public class LongAccumulatorDemo {

    @SneakyThrows
    public static void main(String[] args) {
        LongAccumulator longAccumulator = new LongAccumulator((o1, o2) -> o1 + o2, 0);

        CountDownLatch countDownLatch = new CountDownLatch(10);
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                for (int j = 0; j < 10000; j++) {
                    longAccumulator.accumulate(1);
                }
                countDownLatch.countDown();
            }).start();
        }
        countDownLatch.await();
        log.info("longAdder:{}", longAccumulator.get());
    }
}
